<?php

/**
 * This is the model class for table "{{upload}}".
 *
 * The followings are the available columns in table '{{upload}}':
 * @property string $id
 * @property string $title
 * @property string $name
 * @property string $ext
 * @property string $type
 * @property integer $size
 * @property string $updatetime
 * @property string $updateuserid
 * @property string $createtime
 * @property string $createuserid
 *
 * The followings are the available model relations:
 * @property UploadArticle[] $uploadArticles
 */
class Upload extends ActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Upload the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{upload}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, name, ext, type, size', 'required'),
			array('size', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>255),
			array('name, type', 'length', 'max'=>32),
			array('ext', 'length', 'max'=>8),
			array('updateuserid, createuserid', 'length', 'max'=>11),
			array('createtime, updatetime, updateuserid, createuserid', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, name, ext, type, size, updatetime, updateuserid, createtime, createuserid', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'uploadArticles' => array(self::MANY_MANY, 'Article', '{{upload_article}}(uploadid,articleid)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'name' => 'Name',
			'ext' => 'Ext',
			'type' => 'Type',
			'size' => 'Size',
			'updatetime' => 'Updatetime',
			'updateuserid' => 'Updateuserid',
			'createtime' => 'Createtime',
			'createuserid' => 'Createuserid',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('ext',$this->ext,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('size',$this->size);
		$criteria->compare('updatetime',$this->updatetime,true);
		$criteria->compare('updateuserid',$this->updateuserid,true);
		$criteria->compare('createtime',$this->createtime,true);
		$criteria->compare('createuserid',$this->createuserid,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function seed()
        {
          list($usec, $sec) = explode(' ', microtime());
          return (float) $sec + ((float) $usec * 100000);
        }
        
        public function encrypt($max = 32)
        {
            mt_srand($this->seed());
            $hash = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $length = strlen($hash);
            $name = '';
            for($i = 0; $i < $max; $i++){
                $rand = mt_rand(0,$length - 1);
                $name .= $hash[$rand];
            }
            
            return $name;
        }
}